簡體   English   中英

Select 來自文本文件的隨機數據並將其放在 Excel 電子表格中的特定位置

[英]Select a random data from a text file and put it in a specific locations in an Excel spreadsheet

我有一個問題:我怎樣才能 select 從文件中獲取一個隨機數據並將其放入.csv文件中? 我知道您需要設置路徑以在計算機中定位文件,但我想知道何時使用random()方法或其他方法來使用 random,因為在 C# 中,隨機主要用於整數和雙精度數。

例如:我在文本文件中有這個名稱列表:

Kenneth
Samuel
Samantha
Catherine
Danielle
Jonathan
Ellen
Valentin
Christopher
Edward

現在,我想將其中一個名稱放入.CSV文件中的特定位置。 這是我迄今為止嘗試過的:

namespace ConsoleApp
{
    class Program
    {

        public static List<RecordStructure> CSVRecords = new List<RecordStructure>();

        static void Main(string[] args)
        {
            Console.WriteLine("Data Mask Process");
            Console.WriteLine("**************************");

            CSVRecords.AddRange(ReadCSVFile());

            // some things I wrote for a work :)
            string contentF_names = File.ReadAllText(@"C:\\path");
            string contentM_names = File.ReadAllText(@"C:\\path");
            string contentNames = File.ReadAllText(@"C:\\path");
            string contentPlaces = File.ReadAllText(@"C\\path");

            // For the random part
            Random r = new Random();

            var line = contentF_names[r.Next(contentF_names.Length)];

            // Printing and ending

            Console.WriteLine(CSVRecords[1].firstname);
            Console.WriteLine(CSVRecords[1].lastname);
            Console.WriteLine(CSVRecords[1].city);

            Console.WriteLine("**************************");
            Console.WriteLine("End of Process.");
            Console.ReadKey();
        }

        public static List<RecordStructure> ReadCSVFile()
        {
            List<RecordStructure> RecordList = new List<RecordStructure>();

            StreamReader reader = new StreamReader(File.OpenRead(@"path"));

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();

                if (!String.IsNullOrWhiteSpace(line))
                {
                    string[] values = line.Split(',');

                    RecordList.Add(new RecordStructure
                                       {
                                           firstname = values[0],
                                           lastname = values[1],
                                           company_name = values[2],
                                           city = values[3]
                                       });
            }
        }

        return RecordList;
    }
}

class RecordStructure
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string company_name { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string state { get; set; }
    public string zip { get; set; }
    public string phone1 { get; set; }
    public string phone2 { get; set; }
    public string email { get; set; }
    public string web { get; set; }
}

由於您正在處理CVS文件,因此您可以將結果轉換為ArrayList ,然后將其indexRandom一起使用以獲取隨機索引。

例子:

var contentF_names = "Kenneth,Samuel,Samantha,Catherine,Danielle,Jonathan,Ellen,Valentin,Christopher,Edward";

// ToList
var contentF_namesArray = contentF_names.Split(',').ToList();

//For the random part

Random r = new Random();

// Get Random index between 0 and the total number of elements in the list
var randomIndex = r.Next(0, contentF_namesArray.Count);

// Use the random index to get an element from the list.
var randomFirstName = contentF_namesArray[randomIndex];

randomFirstName將保存隨機名稱,並且每次運行時。 您可以使用循環來獲取 x 個隨機名稱。

例子:

/*
    Get Five Random Names 
*/

for (int x =0; x <= 5; x++)
{
    // Get Random index between 0 and the total number of elements in the list
    var randomIndex = r.Next(0, contentF_namesArray.Count);

    // Use the random index to get an element from the list.
    var randomFirstName = contentF_namesArray[randomIndex];

    Console.WriteLine(randomFirstName);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM